package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.ImageViewPagerActivity;
import quq.missq.beans.AlbumBean;
import quq.missq.beans.Attach;
import quq.missq.utils.ImageLoadInterface;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.UIHelper;

/**
 * Created by River on 8/27/15.
 */
public class PhotoAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_SUBSCRIBLE = 1;

    private Context mContext;
    private LinkedList<Attach> results;
    private LayoutInflater mInflater;


    public PhotoAlbumAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<Attach> results){
        this.results = results;
    }

    public LinkedList<Attach> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_SUBSCRIBLE){
            View view = mInflater.inflate(R.layout.photo_album_adapter,null);
            return new AtMeHolder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,final int position) {
        if (viewHolder instanceof AtMeHolder){
            final AtMeHolder holder = (AtMeHolder)viewHolder;
            final Attach result = results.get(position);
//            float halfScreenWidth = UIHelper.getScreenWidth(mContext)/2;
//            float bitmapWidth = result.getWidth();
//            float scale = halfScreenWidth/bitmapWidth;
//            int realHeight =(int)(result.getHeight()*scale);
//            holder.photo.setLayoutParams(new RelativeLayout.LayoutParams(UIHelper.getScreenWidth(mContext) / 2, realHeight));

            holder.photo.setImageResource(R.mipmap.empty_photo);
//            ImageLoadUtil.loadImage(holder.photo,Constants.IMAGE_HOST+result.getThumbnail(),R.mipmap.empty_photo);
            ImageLoadUtil.loadImage(holder.photo,Constants.IMAGE_HOST+result.getPreview(),R.mipmap.empty_photo);
            holder.photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ImageViewPagerActivity.class);
                    intent.putExtra("position",position);
                    ArrayList<String> urls = new ArrayList<>();
                    for (int i = 0;i < results.size();i++){
                        urls.add(Constants.IMAGE_HOST+results.get(i).getOrigin());
                    }
                    intent.putExtra("netImageUrls",urls);
                    mContext.startActivity(intent);
                }
            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_SUBSCRIBLE;
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class AtMeHolder extends RecyclerView.ViewHolder{

        ImageView photo;

        public AtMeHolder(View itemView) {
            super(itemView);
            photo = (ImageView)itemView.findViewById(R.id.photo);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
